package ar.drools.apuestas.main;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.common.AgendaItem;
import org.drools.event.rule.AfterActivationFiredEvent;
import org.drools.event.rule.DefaultAgendaEventListener;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.FactHandle;

import ar.drools.apuestas.modelo.Piloto;
import ar.drools.apuestas.utils.XMLParser;

public class Sistema {

	public static List<Piloto> obtenerPrediccion() {

		KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kBuilder.add(ResourceFactory.newInputStreamResource(Sistema.class.getResourceAsStream("/rules-bet.drl")), ResourceType.DRL);

		KnowledgeBaseConfiguration kBaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();

		KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(kBaseConfiguration);
		kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());

		StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession();
		kSession.addEventListener(new DefaultAgendaEventListener() {
			@Override
			public void afterActivationFired(AfterActivationFiredEvent event) {
				super.afterActivationFired(event);
				AgendaItem item = (AgendaItem) event.getActivation();
				try {
					Piloto p = (Piloto) event.getActivation().getObjects().get(0);
					System.out.println("Regla | Nombre: " + item.getRule().getName() + " - Piloto: " + p.getNombre());
				} catch (Exception e) {
				}

			}
		});

		List<Piloto> listaPilotos = insertFacts(kSession);
		kSession.dispose();
		return listaPilotos;

	}

	private static List<Piloto> insertFacts(StatefulKnowledgeSession kSession) {
		FactHandle ruleHandle;
		List<Piloto> listaPilotos = XMLParser.obtenerPilotosConDatos();

		for (Piloto piloto : listaPilotos) {
			kSession.getAgenda().getAgendaGroup("procesamientoDatos").setFocus();
			ruleHandle = kSession.insert(piloto);
			kSession.fireAllRules();
			kSession.retract(ruleHandle);

			kSession.getAgenda().getAgendaGroup("obtenerResultados").setFocus();
			ruleHandle = kSession.insert(piloto);
			kSession.fireAllRules();
			kSession.retract(ruleHandle);

			kSession.getAgenda().getAgendaGroup("resultadoFinal").setFocus();
			kSession.insert(piloto);
			kSession.fireAllRules();
		}

		ordernarPorCategoria(listaPilotos);
		return listaPilotos;
	}

	public static void ordernarPorCategoria(List<Piloto> listaPilotos) {
		Collections.sort(listaPilotos, new Comparator<Piloto>() {
			@Override
			public int compare(final Piloto p1, final Piloto p2) {
				return p1.getResultado().compareTo(p2.getResultado());
			}
		});
	}
}
